//
//  AppDelegate.m
//  RongCloudDemo
//
//  Created by 杜立召 on 15/4/18.
//  Copyright (c) 2015年 dlz. All rights reserved.
//

#import "AppDelegate.h"

#import <UIKit/UIKit.h>
#import <RongIMLib/RongIMLib.h>
#import "ViewController.h"
#import <RongIMKit/RongIMKit.h>
#import "XiaoxiViewController.h"
#import "LianxirenViewController.h"
#import "PenyouquanViewController.h"
#import "ToubaoViewController.h"

#import "ChatListViewController.h"
#define RONGCLOUD_IM_APPKEY @"lmxuhwagxy46d" //请换成您的appkey
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    //初始化融云SDK
    [[RCIM sharedRCIM] initWithAppKey:RONGCLOUD_IM_APPKEY];
    
#ifdef __IPHONE_8_0
    // 在 iOS 8 下注册苹果推送，申请推送权限。
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge
                                                                                         |UIUserNotificationTypeSound
                                                                                         |UIUserNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#else
    // 注册苹果推送，申请推送权限。
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
#endif
    
    // 初始化 ViewController。
    
    XiaoxiViewController *root = [[XiaoxiViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
    //    self.window.rootViewController = nav;
    //创建一个底部标签
    UIImage *image = [UIImage imageNamed:@"32"];
    UIImage *image1 = [UIImage imageNamed:@"321"];
    //防止图片被渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"消息" image:image selectedImage:image1];
    
    
    
    LianxirenViewController *root1 = [[LianxirenViewController alloc] init];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:root1];
    //    self.window.rootViewController = nav;
    //创建一个底部标签
    UIImage *image2 = [UIImage imageNamed:@"联系人"];
    UIImage *image3 = [UIImage imageNamed:@"联系人黄"];
    //防止图片被渲染
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image3 = [image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"联系人" image:image2 selectedImage:image3];
    
    
    PenyouquanViewController *root2 = [[PenyouquanViewController alloc] init];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:root2];
    //    self.window.rootViewController = nav;
    //创建一个底部标签
    UIImage *image4 = [UIImage imageNamed:@"朋友圈"];
    UIImage *image5 = [UIImage imageNamed:@"朋友圈黄"];
    //防止图片被渲染
    image4 = [image4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image5 = [image5 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"朋友圈" image:image4 selectedImage:image5];
    
    
    
    ToubaoViewController *root3= [[ToubaoViewController alloc] init];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:root3];
    //    self.window.rootViewController = nav;
    //创建一个底部标签
    UIImage *image6 = [UIImage imageNamed:@"保险"];
    UIImage *image7 = [UIImage imageNamed:@"保险黄"];
    //防止图片被渲染
    image6 = [image6 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image7 = [image7 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"投保" image:image6 selectedImage:image7];
    
    
    
    
    
    UITabBarController *tabbarcon = [[UITabBarController alloc] init];
    tabbarcon.tabBar.tintColor = [UIColor colorWithRed:(255 / 255.0f) green:(132 / 255.0f) blue:(3 / 255.0f) alpha:1];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 49)];
    backView.backgroundColor = [UIColor colorWithRed:(246 / 255.0f) green:(246 / 255.0f) blue:(246 / 255.0f) alpha:1];
    [tabbarcon.tabBar insertSubview:backView atIndex:0];
    tabbarcon.tabBar.opaque = YES;
    //向uitabbarcontroller中添加标签
    tabbarcon.viewControllers = [NSArray arrayWithObjects:nav, nav1, nav2, nav3, nil];
    //设置uitabbarcontro为widow跟视图
    self.window.rootViewController = tabbarcon;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    UIFont *font = [UIFont systemFontOfSize:19.f];
    NSDictionary *textAttributes = @{
                                     NSFontAttributeName : font,
                                     NSForegroundColorAttributeName : [UIColor whiteColor]
                                     };
    [[UINavigationBar appearance] setTitleTextAttributes:textAttributes];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance]
     setBarTintColor:[UIColor colorWithRed:(246 / 255.0f) green:(246 / 255.0f) blue:(246 / 255.0f) alpha:1]];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(didReceiveMessageNotification:)
     name:RCKitDispatchMessageNotification
     object:nil];
    [[RCIM sharedRCIM] setConnectionStatusDelegate:self];
    return YES;
}
/**
 *  将得到的devicetoken 传给融云用于离线状态接收push ，您的app后台要上传推送证书
 *
 *  @param application <#application description#>
 *  @param deviceToken <#deviceToken description#>
 */
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *token =
    [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"
                                                           withString:@""]
      stringByReplacingOccurrencesOfString:@">"
      withString:@""]
     stringByReplacingOccurrencesOfString:@" "
     withString:@""];
    
    [[RCIMClient sharedRCIMClient] setDeviceToken:token];
}


/**
 *  网络状态变化。
 *
 *  @param status 网络状态。
 */
- (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status {
    if (status == ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"提示"
                              message:@"您"
                              @"的帐号在别的设备上登录，您被迫下线！"
                              delegate:nil
                              cancelButtonTitle:@"知道了"
                              otherButtonTitles:nil, nil];
        [alert show];
        ViewController *loginVC = [[ViewController alloc] init];
        UINavigationController *_navi =
        [[UINavigationController alloc] initWithRootViewController:loginVC];
        self.window.rootViewController = _navi;
    }
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
